package com.example.shop_2304.getPre

import androidx.lifecycle.viewModelScope
import com.example.base.entity.CreateOrderEntity
import com.example.base.entity.GetPreOrderEntity
import com.example.base.entity.PayResultEntity
import com.example.base.entity.Res
import com.example.base.mvvm.BaseViewModel
import com.example.base.param.PCreateOrder
import com.example.base.param.PPay
import com.example.base.utils.SingleLiveData
import kotlinx.coroutines.launch

class GetPreViewModel:BaseViewModel() {
    //加载预下单
    val getPreSuccess = SingleLiveData<Res<GetPreOrderEntity>>()
    val getPreFail = SingleLiveData<String>()
    fun getPreOrder(preOrderNo:String){
        viewModelScope.launch {
            try {
                getPreSuccess.postValue(apiService.getPreOrder(preOrderNo))
            } catch (e: Exception) {
                getPreFail.postValue(e.message.toString())
            }
        }
    }

    //创建订单
    val createSuccess = SingleLiveData<Res<CreateOrderEntity>>()
    val createFail = SingleLiveData<String>()
    fun createOrder(pCreateOrder: PCreateOrder){
        viewModelScope.launch {
            try {
                createSuccess.postValue(apiService.createOrder(pCreateOrder))
            } catch (e: Exception) {
                createFail.postValue(e.message.toString())
            }
        }
    }
    //订单支付
    val paySuccess = SingleLiveData<Res<PayResultEntity>>()
    val payFail = SingleLiveData<String>()
    fun pay(pPay: PPay){
        viewModelScope.launch {
            try {
                paySuccess.postValue(apiService.pay(pPay))
            } catch (e: Exception) {
                payFail.postValue(e.message.toString())
            }
        }
    }
}